/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import au.com.bytecode.opencsv.CSVReader;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import lombok.extern.slf4j.Slf4j;
import org.jboss.resteasy.client.ClientRequest;
import org.jboss.resteasy.client.ClientResponse;
import java.util.Date;
import java.text.SimpleDateFormat;

/**
 *
 * @author khaleddaham
 */
@Slf4j
public class RESTImportCapacity {

    private static ClientRequest request = null;
    private static final String T_CAPACITY = "http://localhost:8080/capacity-1.0-SNAPSHOT/capacity/";
    private static final String T_ADD_CAPACITY = T_CAPACITY + "";
    private static final ArrayList<String[]> productReferences = new ArrayList<String[]>();

    public static void main(String[] args) {
        try {
            readProductReferences("import/utbudspunkter.csv");

            //VantetidID,AtgardID,Atgardnamn,Atgardtypnamn,VardgivarNamn,VardgivarDelNamn,VantetidTomDatum,VtidMottag,VtidMottagFortur,VtidOperationMottag,VtidOperationMottagFortur,VtidOperation,VtidOperationFortur,UppgAnsvarig,AndradDatum,AndradSign,Fritext
            String csvFilename = "import/capacity.csv";
//            CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
            BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(csvFilename), "UTF8"));        
            CSVReader csvReader = new CSVReader(in); 
            
            String[] row = null;
            while ((row = csvReader.readNext()) != null) {
                String careGiverName = row[4];
                String careGiverPartName = row[5];
                String product = row[2];

                String validTo = row[6];
                //VtidMottag,VtidMottagFortur,VtidOperationMottag,VtidOperationMottagFortur,VtidOperation,VtidOperationFortur
                String tType1 = row[7];
                String tType2 = row[8];
                String tType3 = row[9];
                String tType4 = row[10];
                String tType5 = row[11];
                String tType6 = row[12];

                String hsaId = getHsaId(careGiverName, careGiverPartName);
                if (hsaId != null) {
//                System.out.println("produkt: " + product + ", " + careGiverName + ", hsaId: " + hsaId);
//                System.out.println("\tcapacities: " + row[6]);
                    if (!tType1.isEmpty()) {
//                        addCapacity(hsaId, product, "tType1", "oid.sll.vantetider", tType1, validTo);
                        addCapacity(hsaId, product, "1", "oid.sll.vantetider", tType1, validTo);
                    }
                    if (!tType2.isEmpty()) {
//                        addCapacity(hsaId, product, "tType2", "oid.sll.vantetider", tType2, validTo);
                        addCapacity(hsaId, product, "2", "oid.sll.vantetider", tType2, validTo);
                    }
                    if (!tType3.isEmpty()) {
//                        addCapacity(hsaId, product, "tType3", "oid.sll.vantetider", tType3, validTo);
                        addCapacity(hsaId, product, "3", "oid.sll.vantetider", tType3, validTo);
                    }
                    if (!tType4.isEmpty()) {
//                        addCapacity(hsaId, product, "tType4", "oid.sll.vantetider", tType4, validTo);
                        addCapacity(hsaId, product, "4", "oid.sll.vantetider", tType4, validTo);
                    }
                    if (!tType5.isEmpty()) {
//                        addCapacity(hsaId, product, "tType5", "oid.sll.vantetider", tType5, validTo);
                        addCapacity(hsaId, product, "5", "oid.sll.vantetider", tType5, validTo);
                    }
                    if (!tType6.isEmpty()) {
//                        addCapacity(hsaId, product, "tType6", "oid.sll.vantetider", tType6, validTo);
                        addCapacity(hsaId, product, "6", "oid.sll.vantetider", tType6, validTo);
                    }

                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private static String getHsaId(String careGiverName, String careGiverPartName) {
        System.out.println(careGiverName + ", " + careGiverPartName);

        for (int i = 0; i < productReferences.size(); i++) {
            if (productReferences.get(i)[1].equals(careGiverName) && productReferences.get(i)[3].equals(careGiverPartName)) {
                log.info("Found prodref: " + productReferences.get(i)[2] + ", for: " + careGiverName + ", " + careGiverPartName);
                return productReferences.get(i)[5];

            }
        }
        return null;
    }

    private static void readProductReferences(String csvFilename) throws FileNotFoundException, IOException {
//        CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
            BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(csvFilename), "UTF8"));        
            CSVReader csvReader = new CSVReader(in); 
        String[] row = null;
        while ((row = csvReader.readNext()) != null) {
            productReferences.add(row);
        }
    }

    /**
     * @QueryParam("hsaID") String hsaID,
     * @QueryParam("product") String product,
     * @QueryParam("code") String code,
     * @QueryParam("codeSystem") String codeSystem,
     * @QueryParam("value") Long value,
     * @QueryParam("validTo") Long validTo)
     * @param name
     * @throws Exception
     */
    private static void addCapacity(String hsaID, String product, String code, String codeSystem, String value, String validTo) throws Exception {
        request = new ClientRequest(T_CAPACITY);
        request.accept("application/json");

        SimpleDateFormat originalFormat = new SimpleDateFormat("MM/dd/yyyy");
        SimpleDateFormat targetFormat = new SimpleDateFormat("yyyyMMddHHmmss");

        Date date = originalFormat.parse(validTo);
        String formattedDate = targetFormat.format(date);

        String input = "{\"hsaID\":\"" + hsaID + "\",\"product\":\"" + product
                + "\",\"code\":\"" + code
                + "\",\"codeSystem\":\"" + "oid.sll.vantetider"
                + "\",\"value\":\"" + value
                + "\",\"validTo\":\"" + formattedDate
                + "\"}";
        System.out.println("Creating codeSystem: " + input);
        request.body("application/json;charset=utf-8", input);
        ClientResponse<String> response = request.post(String.class);

        if (response.getStatus() != 204) {
            System.out.println("Failed : HTTP error code : " + response.getStatus());
        }
    }
}
